﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Aragorn.Consul;
using Consul;

namespace Aragorn.Consul.Clinet
{
    class Program
    {
        static void Main(string[] args)
        {
            var config = new ConsulClientConfiguration();
            config.Address = new Uri("http://127.0.0.1:8500");
            config.WaitTime = new TimeSpan(0, 0, 100);

            var client = new ConsulClient(config);


            AgentServiceRegistration serviceRegistration = new AgentServiceRegistration
            {
                Name = "Test",
                Port = 8080,
                Address = "127.0.0.1",
                Tags = new[] { "services", "testmall"},
                Check = new AgentServiceCheck
                {
                    HTTP = "127.0.0.1/health",
                    Interval = new TimeSpan(0, 0, 10)
                }

                //Check = new AgentServiceCheck
                //{
                //    TTL = TimeSpan.FromSeconds(15),
                //    Status = CheckStatus.Passing
                //}
                //Check = new Program().CheckTtl()

            };



            client.Agent.ServiceRegister(serviceRegistration);

            var sessionRequest = client.Session.Create(new SessionEntry() { TTL = TimeSpan.FromSeconds(10) });

            var id = sessionRequest.Result.Response;

            Console.WriteLine("The services is running!");

             //new Program().Health_Service();

            //Console.ReadKey();

            if (client.Agent.Services().Result.Response.ContainsKey("Test"))
            {
                Console.WriteLine("Test is alive");
            }
            Console.ReadKey();

        }

        public AgentServiceCheck CheckTtl()
        {
            var check = new AgentServiceCheck
            {
                TTL = TimeSpan.FromSeconds(5)
            };

            return check;
        }

        public  void Health_Service()
        {
            var client = new ConsulClient();

            var checks =  client.Health.Service("Test", "", true);

            if (checks.Result.LastIndex==0)
            {
                Console.WriteLine("services is dead!");
            }
            
        }
    }
}
